package com.dhnsoft.game;

import com.dhnsoft.map.GameMap;
import com.dhnsoft.map.MapBuff;
import com.dhnsoft.server.NetClient;
import com.dhnsoft.server.TankServer;
import com.dhnsoft.tank.*;
import com.dhnsoft.util.Audio;
import com.dhnsoft.util.MyUtil;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.List;

import static com.dhnsoft.util.Constant.*;

/**
 * @Description
 * @ClassName GameFrame
 * @Author dhn
 * @date 2020.11.01 19:46
 */

/*
* 游戏的主窗口类
* */
    /**
     * 也是坦克客户端
     * */
public class GameFrame extends Frame implements Runnable{
    //第一次使用的时候加载，而不是类加载的时候加载
    private Image overImg = null;

    //定义一张和屏幕大小一致的图片
    private BufferedImage bufImg=new BufferedImage(FRAME_WIDTH,FRAME_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR);
    //游戏状态
    public static int gameState;
    //菜单指向
    private int menuIndex;
    //定义坦克对象
    public static  MyTank myTank;
    //定义一个单挑坦克对象
    public static SingleTank singleTank;
    //定义坦克对象
    public  static MyTank2 myTank2;
    //敌人的坦克容器
    private static List<EnemyTank> enemies = new ArrayList<>();
    //定义地图相关的内容
    private static GameMap gameMap;
    //标题栏的高度
    public static int titleBarH;
    //用来记录本关卡产生了多少个敌人
    private static int bornEnemyCount;

    //销毁了多少敌人
    public static int killEnemyCount;

    //buff
    private MapBuff mapBuff = new MapBuff();

    NetClient nc = new NetClient(this);

    //服务器上接收的参数
    //StartServer startServer = new StartServer();
        //nc.connect("127.0.0.1", StartServer.TCP_PORT);
    /*
    * 对窗口进行初始化
    * */
    public GameFrame(){
        initFrame();
        initEventListener();
        //启动用于刷新窗口的线程
        new Thread(this).start();

    }
    /**
     * 进入下一关的功能
     * */
    public static void nextLevel() {
        startGame(LevelInfo.getInstance().getLevel()+1);
    }

    //开始过关动画
    public static int flashTime;
    public static final int RECT_WIDTH = 40;
    public static final int RECT_COUNT = FRAME_WIDTH/RECT_WIDTH+1;
    public static boolean isOpen = false;
    public static void startCrossLevel(){
        gameState = STATE_CROSS;
        flashTime = 0;
        isOpen = false;
    }
    //绘制过关动画
    public void drawCross(Graphics g){
        gameMap.drawBK(g);
        if(myTank.getBloodFlag()==0){
            myTank.draw(g);
        }else {
            myTank.draw2(g);
        }

        gameMap.drawCover(g);

        g.setColor(Color.BLACK);
        //关闭百叶窗的效果
        if(!isOpen) {
            for (int i = 0; i < RECT_COUNT; i++) {
                g.fillRect(i * RECT_WIDTH, 0, flashTime, FRAME_HEIGHT);
            }
            //所有的叶片都关闭了
            if (flashTime++ - RECT_WIDTH > 5) {
                isOpen = true;
                //初始化下一个地图
                gameMap.initMap(LevelInfo.getInstance().getLevel()+1);
            }
        }else{
            //开百叶窗的效果
            for (int i = 0; i < RECT_COUNT; i++) {
                g.fillRect(i * RECT_WIDTH, 0, flashTime, FRAME_HEIGHT);
            }
            if(flashTime-- == 0){
                startGame(LevelInfo.getInstance().getLevel());
            }
        }
    }
    /*
    * 对游戏进行初始化
    * */
    private void initGame(){
        gameState=STATE_MENU;
    }
    /*
     * 属性进行初始化
     * */
    private void initFrame(){
        //设置标题
        setTitle(GAME_TITLE);
        //设置窗口大小
        setSize(FRAME_WIDTH,FRAME_HEIGHT);
        //设置窗口的左上表的
        setLocationRelativeTo(null);
        //设置窗口大小不可改变
        setResizable(false);
        //设置窗口可见
        setVisible(true);
        //求标题栏的高度
        titleBarH=getInsets().top;


    }
    private void initEventListener(){
        //注册监听事件
        addWindowListener(new WindowAdapter() {
            //点击关闭按钮的时候，方法会被自动调用
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //添加按键监听事件
        addKeyListener(new KeyAdapter() {
            //按键被按下的时候被回调的方法
            @Override
            public void keyPressed(KeyEvent e) {
                //被按下键的键值
                int keyCode = e.getKeyCode();
                //不同的游戏状态，给出不同的处理的方法
                switch (gameState){
                    case STATE_MENU:
                        keyPressedEventMenu(keyCode);
                        break;
                    case STATE_HELP:
                        keyPressedEventHelp(keyCode);
                        break;
                    case STATE_ABOUT:
                        keyPressedEventAbout(keyCode);
                        break;
                    case STATE_RUN:
                            keyPressedEventRun2(readtxtFile());
                        //System.out.println("哈哈"+readtxtFile());
                        try {
                            keyPressedEventRun(keyCode);
                        } catch (InterruptedException interruptedException) {
                            interruptedException.printStackTrace();
                        }
                        break;
                    case STATE_OVER:
                        keyPressedEventOver(keyCode);
                        break;
                    case STATE_WIN:
                        KeyPressedEventWin(keyCode);
                        break;
                }
            }

            //游戏通关的按键处理
            private void KeyPressedEventWin(int keyCode) {
                keyPressedEventOver(keyCode);

            }


            //按键被松开的时候被回调的方法
            @Override
            public void keyReleased(KeyEvent e) {
                //被按下键的键值
                int keyCode = e.getKeyCode();
                //不同的游戏状态，给出不同的处理的方法
                if(gameState==STATE_RUN){
                        keyReleasedEventRun2(readtxtFile());
                        keyReleasedEventRun(keyCode);
                }
                if(gameState==STATE_ABOUT){
                    keyReleasedEventRunSingle(keyCode);
                }
            }

            //按键松开的时候，游戏中的处理方法
            private void keyReleasedEventRun(int keyCode) {
                switch (keyCode){
                    case KeyEvent.VK_UP:
                    case KeyEvent.VK_DOWN:
                    case KeyEvent.VK_LEFT:
                    case KeyEvent.VK_RIGHT:
                        myTank.setState(Tank.STATE_STAND);
                        break;
                }
                switch (keyCode){
                    case KeyEvent.VK_W:
                    case KeyEvent.VK_S:
                    case KeyEvent.VK_A:
                    case KeyEvent.VK_D:
                        myTank2.setState(Tank.STATE_STAND);
                        break;
                }

            }
            //按键松开的时候，游戏中的处理方法
            private void keyReleasedEventRunSingle(int keyCode) {
                switch (keyCode){
                    case KeyEvent.VK_UP:
                    case KeyEvent.VK_DOWN:
                    case KeyEvent.VK_LEFT:
                    case KeyEvent.VK_RIGHT:
                        myTank.setState(Tank.STATE_STAND);
                        break;
                }
                switch (keyCode){
                    case KeyEvent.VK_W:
                    case KeyEvent.VK_S:
                    case KeyEvent.VK_A:
                    case KeyEvent.VK_D:
                        singleTank.setState(Tank.STATE_STAND);
                        break;
                }

            }

            //按键松开的时候，游戏中的处理方法
            private void keyReleasedEventRun2(int keyCode) {
                switch (keyCode){
                    case KeyEvent.VK_UP:
                    case KeyEvent.VK_DOWN:
                    case KeyEvent.VK_LEFT:
                    case KeyEvent.VK_RIGHT:
                        myTank2.setState(Tank.STATE_STAND);
                        break;
                }

            }

            /*
            * 游戏结束的按键处理 TODO
            * */
            private void keyPressedEventOver(int keyCode) {
                //结束游戏
                if(keyCode == KeyEvent.VK_ESCAPE){
                    System.exit(0);
                }else if(keyCode == KeyEvent.VK_ENTER){
                    setGameState(STATE_MENU);
                    //很多游戏的操作需要关闭，有些的某些属性需要重置
                    resetGame();
                }
            }
            //重置游戏状态
            private void resetGame(){
                menuIndex = 0;
                //先让自己的坦克的子弹还回对象池
                myTank.bulletsReturn();
                if(myTank2!=null){
                    myTank2.bulletsReturn();
                }
                //销毁自己的坦克
                myTank = null;
                if(myTank2!=null){
                    myTank2=null;
                }

                //清空敌人相关资源
                for (EnemyTank enemy : enemies) {
                    enemy.bulletsReturn();
                }
                enemies.clear();
                //清空地图资源
                gameMap = null;
            }


            /**
             * 游戏运行中的方法
             * */
            private void keyPressedEventRun(int keyCode) throws InterruptedException {
                switch (keyCode){
                    case KeyEvent.VK_UP:
                        myTank.setDir(Tank.DIR_UP);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_DOWN:
                        myTank.setDir(Tank.DIR_DOWN);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_LEFT:
                        myTank.setDir(Tank.DIR_LEFT);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_RIGHT:
                        myTank.setDir(Tank.DIR_RIGHT);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_NUMPAD1:
                        myTank.fire();
                        break;
                    case KeyEvent.VK_Y:
                        if(myTank.getAtkFlag()==1||myTank.getAtkFlag()==2){
                            myTank.setAtkFlag(0);
                            myTank2.setAtkFlag(0);

                        }else {
                            myTank.setAtkFlag(1);
                            myTank2.setAtkFlag(1);
                        }
                        break;
                    case KeyEvent.VK_U:
                        if(myTank.getAtkFlag()==0||myTank.getAtkFlag()==1){
                            myTank.setAtkFlag(2);
                            myTank2.setAtkFlag(2);

                        }else {
                            myTank.setAtkFlag(0);
                            myTank2.setAtkFlag(0);
                        }
                        break;
                    case KeyEvent.VK_O:
                        if(myTank.getBloodFlag()==0){
                            myTank.setBloodFlag(1);
                            myTank.setHp(99999);
                            myTank2.setHp(99999);
                        }else {
                            myTank.setBloodFlag(0);
                            myTank.setHp(99);
                            myTank2.setHp(99);
                        }
                    case KeyEvent.VK_P:
                        for (EnemyTank enemy : enemies) {
                            enemy.die();
                        }
                        Thread.sleep(100);

                        enemies.clear();
                }

                switch (keyCode){
                    case KeyEvent.VK_W:
                        myTank2.setDir(Tank.DIR_UP);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_S:
                        myTank2.setDir(Tank.DIR_DOWN);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_A:
                        myTank2.setDir(Tank.DIR_LEFT);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_D:
                        myTank2.setDir(Tank.DIR_RIGHT);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_J:
                        myTank2.fire();
                        break;
                }
            }

            private void keyPressedEventRun2(int keyCode) {
                switch (keyCode){
                    case KeyEvent.VK_UP:
                        myTank2.setDir(Tank.DIR_UP);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_DOWN:
                        myTank2.setDir(Tank.DIR_DOWN);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_LEFT:
                        myTank2.setDir(Tank.DIR_LEFT);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_RIGHT:
                        myTank2.setDir(Tank.DIR_RIGHT);
                        myTank2.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_SPACE:
                        myTank2.fire();
                        break;
                }
            }

            private void keyPressedEventAbout(int keyCode) {
                switch (keyCode){
                    case KeyEvent.VK_UP:
                        myTank.setDir(Tank.DIR_UP);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_DOWN:
                        myTank.setDir(Tank.DIR_DOWN);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_LEFT:
                        myTank.setDir(Tank.DIR_LEFT);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_RIGHT:
                        myTank.setDir(Tank.DIR_RIGHT);
                        myTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_NUMPAD1:
                        myTank.fire();
                        break;
                }

                switch (keyCode){
                    case KeyEvent.VK_W:
                        singleTank.setDir(Tank.DIR_UP);
                        singleTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_S:
                        singleTank.setDir(Tank.DIR_DOWN);
                        singleTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_A:
                        singleTank.setDir(Tank.DIR_LEFT);
                        singleTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_D:
                        singleTank.setDir(Tank.DIR_RIGHT);
                        singleTank.setState(Tank.STATE_MOVE);
                        break;
                    case KeyEvent.VK_ENTER:
                        //startGame(1);
                        break;
                    case KeyEvent.VK_J:
                        singleTank.fire();
                        break;
                }
            }

            private void keyPressedEventHelp(int keyCode) {
                setGameState(STATE_MENU);
            }
        });
    }
    //重置游戏状态
    private void resetGame(){
        killEnemyCount = 0;
        menuIndex = 0;
        //先让自己的坦克的子弹还回对象池
        myTank.bulletsReturn();
        myTank2.bulletsReturn();
        //销毁自己的坦克
        myTank = null;
        myTank2=null;
        //清空敌人相关资源
        for (EnemyTank enemy : enemies) {
            enemy.bulletsReturn();
        }
        enemies.clear();
        //清空地图资源
        gameMap = null;
    }


    //菜单状态下的按键的处理
    private void keyPressedEventMenu(int keyCode) {
        switch (keyCode){
            case KeyEvent.VK_UP:
            case KeyEvent.VK_U:
                menuIndex--;
                if(menuIndex<0){
                    menuIndex=MENUS.length-1;
                }
                break;
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_D:
                menuIndex++;
                if(menuIndex>MENUS.length-1){
                    menuIndex=0;
                }
                break;
//            case KeyEvent.VK_ENTER:
//                startGame(1);
//                break;
            case KeyEvent.VK_ENTER:
                switch(menuIndex){
                    //TODO
                    case 0:
                        startGame(1);
                        break;
//                    case 1:
//                        //继续游戏。进入选择关卡的界面
//                        break;
                    case 1:
                        startGame2(3);
                        break;
                    case 2:
                        setGameState(STATE_HELP);
                        break;

                    case 3:
                        System.exit(0);
                        break;
                }
                break;
        }
    }
    /*
    * 开始游戏，并加载关卡信息
    * */
    private  static void startGame(int level) {
        if(gameMap == null){
            gameMap = new GameMap();
        }
        gameMap.initMap(level);
        gameState=STATE_RUN;
        killEnemyCount = 0;
        bornEnemyCount = 0;
        //创建创客对象，敌人的坦克对象
        myTank=new MyTank(FRAME_WIDTH/3,FRAME_HEIGHT-Tank.RADIUS*2,Tank.DIR_UP);
        myTank2=new MyTank2(FRAME_WIDTH/3-70,FRAME_HEIGHT-Tank.RADIUS*2,Tank.DIR_UP);

        new Thread(){
            @Override
            public void run() {
                while (true){
                    if(LevelInfo.getInstance().getEnemyCount()>bornEnemyCount&&enemies.size()<MAX_ENEMY_COUNT){
                        Tank enemy = EnemyTank.createEnemy();
                        enemies.add((EnemyTank) enemy);
                        bornEnemyCount++;
                    }
                    try {
                        Thread.sleep(BORN_ENEMY_INTERVAL);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //只有在游戏run状态下才创建敌人坦克
                    if(gameState!=STATE_RUN){
                        break;
                    }

                }
            }
        }.start();
    }
        private  static void startGame2(int level) {
           gameMap = new GameMap();

            gameMap.initMap(level);
            gameState=STATE_ABOUT;
            //创建创客对象，敌人的坦克对象
            myTank=new MyTank(FRAME_WIDTH/3+50,FRAME_HEIGHT-Tank.RADIUS*2,Tank.DIR_UP);
            singleTank = new SingleTank(FRAME_WIDTH/3+50,FRAME_HEIGHT-Tank.RADIUS*2-670,Tank.DIR_DOWN);
            //myTank2=new MyTank2(FRAME_WIDTH/3-70,FRAME_HEIGHT-Tank.RADIUS*2,Tank.DIR_UP);
        }

    /*
    *
    * 是Frame类的方法，继承下来的方法
    * 改方法负责了所有的绘制的内容，所有需要在屏幕中显示的
    * 内容，都要在该方法内调用。该方法不智能主动调用.必须通过调用。
    * */
    @Override
    public void update(Graphics g1){
        //得到图片的画笔
        Graphics g = bufImg.getGraphics();
        g.setFont(GAME_FONT);
        switch (gameState){
            case STATE_MENU:
                drawMenu(g);
                break;
            case STATE_HELP:
                drawHelp(g);
                break;
            case STATE_ABOUT:
                drawAbout(g);
                break;
            case STATE_RUN:
                drawRun(g);
                break;
            case STATE_OVER:
                drawOver(g,"过关失败!");
                break;
            case STATE_WIN:
                drawWin(g);
                break;
            case STATE_CROSS:
                drawCross(g);
                break;
        }
        g1.drawImage(bufImg,0,0,null);
    }

    //游戏运行状态的绘制内容
    private void drawRun(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0,0,FRAME_WIDTH,FRAME_HEIGHT );

        //绘制地图的碰撞层
        gameMap.drawBK(g);
        //绘制攻击buff
        gameMap.drawATK(g);
        //绘制速度buff
        gameMap.drawSpeed(g);
        drawEnemies(g);
        if(myTank.getBloodFlag()==1){
            myTank.draw2(g);
            //System.out.println("id11:"+myTank.id);
            myTank2.draw2(g);
        }else {
            myTank.draw(g);
            //System.out.println("id11:"+myTank.id);
            myTank2.draw(g);
        }

        //绘制地图的遮挡层
        gameMap.drawCover(g);
        //爆炸效果
        drawExplodes(g);
        //子弹和坦克的碰撞方法
        bulletCollideTank();
        //子弹和所有地图块的碰撞
        bulletAndTankCollideMapTile();
    }
    //绘制所有的敌人坦克,如果敌人已经死亡，从容器中移出
    private void drawEnemies(Graphics g){
        for (int i = 0; i < enemies.size(); i++) {
            Tank enemy = enemies.get(i);
            if(enemy.isDie()){
                enemies.remove(i);
                i--;
                continue;
            }
            enemy.draw(g);
        }
    }
    private void drawWin(Graphics g){
        drawOver(g,"游戏通关！");
//        g.setColor(Color.WHITE);
//        g.drawString("游戏通关!",FRAME_WIDTH/2-30,50);
    }

    /*
     * 绘制游戏结束方法
     * */
    private void drawOver(Graphics g,String str) {
        //保证只加载一次
        if(overImg == null){
            overImg = MyUtil.createImage("images/over.jpg");
        }
        g.setColor(Color.BLACK);
        g.fillRect(0,0,FRAME_WIDTH,FRAME_HEIGHT);

        int imgW = overImg.getWidth(null);
        int imgH = overImg.getHeight(null);
        g.drawImage(overImg,FRAME_WIDTH-imgW>>1,FRAME_HEIGHT-imgH>>1,null);
        //添加按键提示信息
        g.setColor(Color.WHITE);
        g.drawString(OVER_STR0,10,FRAME_HEIGHT-40);
        g.drawString(OVER_STR1,FRAME_WIDTH-250,FRAME_HEIGHT-40);
        //失败文字
        g.setColor(Color.WHITE);
        g.drawString(str,FRAME_WIDTH/2-30,70);
    }

    private void drawAbout(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0,0,FRAME_WIDTH,FRAME_HEIGHT );

        //绘制地图的碰撞层
        gameMap.drawBK(g);
        //绘制攻击buff
        gameMap.drawATK(g);
        //绘制速度buff
        gameMap.drawSpeed(g);
        //drawEnemies(g);
        myTank.draw(g);
        singleTank.draw(g);

        //System.out.println("id11:"+myTank.id);
        //myTank2.draw(g);
        //绘制地图的遮挡层
        gameMap.drawCover(g);
        //爆炸效果
        drawExplodes2(g);
        //子弹和坦克的碰撞方法
        bulletCollideTank2();
        //子弹和所有地图块的碰撞
        bulletAndTankCollideMapTile2();
    }
    private Image helpImg;
    private void drawHelp(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0,0,FRAME_WIDTH,FRAME_HEIGHT);
        if(helpImg == null){
            helpImg = MyUtil.createImage("images/help.jpg");
        }
        int width = helpImg.getWidth(null);
        int height = helpImg.getHeight(null);

        int x = FRAME_WIDTH - width >>1;
        int y = FRAME_HEIGHT - height >> 1;
        g.drawImage(helpImg,x,y,null);

        g.setColor(Color.WHITE);
        g.drawString("任意键继续",10,FRAME_HEIGHT-10);
    }

    /**
     * 绘制菜单状态下的内容
     * @param g   画笔对象，系统提供的
     */
    private void drawMenu(Graphics g){
        //绘制黑色的背景
        g.setColor(Color.BLACK);
        g.fillRect(0,0,FRAME_WIDTH,FRAME_HEIGHT);

        final int STR_WIDTH=76;
        int x=FRAME_WIDTH-STR_WIDTH>>1;
        int y=FRAME_HEIGHT/3;
        final int DIS=50;
        g.setColor(Color.WHITE);
        for (int i = 0; i < MENUS.length ; i++) {
            if(i==menuIndex){//选中的菜单颜色设置为红色。
                g.setColor(Color.RED);
            }else {//其他颜色为白色
                g.setColor(Color.WHITE);
            }
            g.drawString(MENUS[i],x,y+DIS*i);
        }
    }

    @Override
    public void run() {

        while (true){
            repaint();
            try {
                Thread.sleep(REPAINT_INTERVAL);
                //
//                if(TankServer.getList()!=0){
//                    System.out.println(TankServer.getList());
//                }
                if(TankServer.INSTANCE.getKeyCode()!="38"){
                    //System.out.println(TankServer.INSTANCE.getKeyCode());
            }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //所有的子弹和地图块的碰撞
    private void bulletAndTankCollideMapTile(){
        //自己的坦克的子弹和地图块的碰撞
        myTank.bulletsCollideMapTiles(gameMap.getTiles());
        myTank2.bulletsCollideMapTiles(gameMap.getTiles());
        for (EnemyTank enemy : enemies) {
            //敌人的坦克和地图的碰撞
            enemy.bulletsCollideMapTiles(gameMap.getTiles());
        }
        //坦克和地图的碰撞
        if(myTank.isCollideTile(gameMap.getTiles())){
            myTank.back();
        }
        //坦克和地图的碰撞
        if(myTank2.isCollideTile(gameMap.getTiles())){
            myTank2.back();
        }

        //坦克和buff的碰撞
        if(myTank.isCollideBuff(gameMap.getBuff())){
            mapBuff = gameMap.getBuff();
            myTank.setAtkFlag(1);
            mapBuff.setAtkVisible(false);
            new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
            System.out.println(mapBuff);
        }
        //坦克和buff的碰撞
        if(myTank2.isCollideBuff(gameMap.getBuff())){
            mapBuff = gameMap.getBuff();
            myTank2.setAtkFlag(1);
            mapBuff.setAtkVisible(false);
            new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
            System.out.println(mapBuff);
        }

        //坦克和速度buff的碰撞
        if(myTank.isCollideSpeedBuff(gameMap.getBuff())){
            mapBuff = gameMap.getBuff();
            myTank.setSpeed(15);
            mapBuff.setSpeedVisible(false);
            new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
            System.out.println(myTank);
        }
        //坦克和速度buff的碰撞
        if(myTank2.isCollideSpeedBuff(gameMap.getBuff())){
            mapBuff = gameMap.getBuff();
            myTank2.setSpeed(15);
            mapBuff.setSpeedVisible(false);
            new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
            //System.out.println(mapBuff);
        }
        //敌人的坦克和地图的碰撞
        for (EnemyTank enemy : enemies) {
            if(enemy.isCollideTile(gameMap.getTiles())){
                enemy.back();;
            }
        }
        //清理所有的被销毁的地图块
        gameMap.clearDestroyTile();

    }

        //所有的子弹和地图块的碰撞
        private void bulletAndTankCollideMapTile2(){
            //自己的坦克的子弹和地图块的碰撞
            myTank.bulletsCollideMapTiles(gameMap.getTiles());
            singleTank.bulletsCollideMapTiles(gameMap.getTiles());
//            for (EnemyTank enemy : enemies) {
//                //敌人的坦克和地图的碰撞
//                enemy.bulletsCollideMapTiles(gameMap.getTiles());
//            }
            //坦克和地图的碰撞
            if(myTank.isCollideTile(gameMap.getTiles())){
                myTank.back();
            }
            //坦克和地图的碰撞
            if(singleTank.isCollideTile(gameMap.getTiles())){
                singleTank.back();
            }
            //坦克和地图的碰撞


            //坦克和buff的碰撞
            if(myTank.isCollideBuff(gameMap.getBuff())){
                mapBuff = gameMap.getBuff();
                myTank.setAtkFlag(1);
                mapBuff.setAtkVisible(false);
                new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
                //System.out.println(mapBuff);
            }
            //坦克和buff的碰撞
            if(singleTank.isCollideBuff(gameMap.getBuff())){
                mapBuff = gameMap.getBuff();
                singleTank.setAtkFlag(1);
                mapBuff.setAtkVisible(false);
                new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
                //System.out.println(mapBuff);
            }

            //坦克和速度buff的碰撞
            if(myTank.isCollideSpeedBuff(gameMap.getBuff())){
                mapBuff = gameMap.getBuff();
                myTank.setSpeed(15);
                mapBuff.setSpeedVisible(false);
                new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
                System.out.println(myTank);
            }
            //坦克和速度buff的碰撞
            if(singleTank.isCollideSpeedBuff(gameMap.getBuff())){
                mapBuff = gameMap.getBuff();
                singleTank.setSpeed(15);
                mapBuff.setSpeedVisible(false);
                new Thread(()->new Audio("audio/atkBuff.wav").play()).start();
                //System.out.println(mapBuff);
            }
            //敌人的坦克和地图的碰撞
            for (EnemyTank enemy : enemies) {
                if(enemy.isCollideTile(gameMap.getTiles())){
                    enemy.back();;
                }
            }
            //清理所有的被销毁的地图块
            gameMap.clearDestroyTile();

        }


    private void bulletCollideTank(){
        //我的坦克的子弹和所有的敌人的碰撞
        for (EnemyTank enemy : enemies) {
            enemy.collideBullets(myTank.getBullets());
        }
        //我的坦克的子弹和所有的敌人的碰撞
        for (EnemyTank enemy : enemies) {
            enemy.collideBullets(myTank2.getBullets());
        }
        //敌人的坦克的子弹和我的坦克的碰撞
        for (EnemyTank enemy : enemies) {
            myTank.collideBullets(enemy.getBullets());
        }
        //敌人的坦克的子弹和我的坦克的碰撞
        for (EnemyTank enemy : enemies) {
            myTank2.collideBullets(enemy.getBullets());
        }
    }
        //我的坦克和solo坦克碰撞
        private void bulletCollideTank2(){
            //我的坦克的子弹和所有的敌人的碰撞
                singleTank.collideBullets(myTank.getBullets());
            //敌人的坦克的子弹和我的坦克的碰撞
                myTank.collideBullets(singleTank.getBullets());
        }
    //所有的坦克上的爆炸效果
    private void drawExplodes(Graphics g){
        for (EnemyTank enemy : enemies) {
            enemy.drawExplodes(g);
        }
        myTank.drawExplodes(g);
        myTank2.drawExplodes(g);
    }
    //单挑时候的爆炸效果
    private void drawExplodes2(Graphics g){
        singleTank.drawExplodes(g);
        myTank.drawExplodes(g);
    }
    //获得游戏状态和修改游戏状态
    public static void setGameState(int gameState) {
        GameFrame.gameState = gameState;
    }

    /*
    * 游戏是否是最后一关
    * */
    public static boolean  isLastLevel(){
        int currentLevel = LevelInfo.getInstance().getLevel();
        int levelCount = GameInfo.getLevelCount();
        return currentLevel==levelCount;


    }
    /*
    * 判断是否过关了
    * */
    public static boolean isCrossLevel(){
        //敌人的死的数量和当前总关卡一致
        return killEnemyCount == LevelInfo.getInstance().getEnemyCount();
    }

        public static MyTank getMyTank() {
            return myTank;
        }

        public static void setMyTank(MyTank myTank) {
            GameFrame.myTank = myTank;
        }


        //读取txt内容
        public static int readtxtFile() {
            File src = new File("D:/a.txt");
            String sResult="";
            String s = "";
            try {
                InputStreamReader reader=new InputStreamReader(new FileInputStream(src),"gbk");
                BufferedReader br=new BufferedReader(reader);
                s=br.readLine();
            }catch (Exception e) {
                e.printStackTrace();
            }
            return Integer.parseInt(s);
        }


}
